package jy.controller;

import javax.servlet.http.HttpServletRequest;

import jy.model.Tdata;
import jy.pageModel.Data;
import jy.pageModel.DataGrid;
import jy.pageModel.Json;
import jy.pageModel.PageHelper;
import jy.service.DataServiceI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户控制器
 * 
 * @author 孙宇
 * 
 */
@Controller
@RequestMapping("/dataController")
public class DataController extends BaseController {

	@Autowired
	private DataServiceI dataService;

	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {

		request.setAttribute("coreDataList", dataService.getDataList());
				
		return "/sjgl/datamanager";
	}

	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(Tdata data, PageHelper ph) {
		return dataService.dataGrid(data, ph);
	}
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, String dataname) {
		Tdata d = dataService.get(dataname);
		request.setAttribute("data", d);
		return "/sjgl/editData";
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Data data) {
		Json j = new Json();
		try {
			dataService.edit(data);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setObj(data);
		} catch (Exception e) {
			// e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
}
